package ch14;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Calculate extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane mainpane = new BorderPane();
        HBox numbers = new HBox();
        Label n1 = new Label("Number1: ");
        Label n2 = new Label("Number2: ");
        Label n3 = new Label("Result: ");
        TextField in1 = new TextField("1.00");
        TextField in2 = new TextField("2.00");
        TextField in3 = new TextField("");
        numbers.getChildren().addAll(n1, in1, n2, in2, n3, in3);
        mainpane.setCenter(numbers);
        HBox buttons = new HBox();
        Button add = new Button("加");
        Button minus = new Button("减");
        Button multiply = new Button("乘");
        Button divide = new Button("除");
        buttons.getChildren().addAll(add, minus, multiply, divide);
        mainpane.setBottom(buttons);
        BorderPane.setAlignment(buttons, Pos.TOP_CENTER);
        Scene scene = new Scene(mainpane);
        primaryStage.setScene(scene);
        primaryStage.show();
        add.setOnMousePressed(e -> {
            String[] n1list = in1.getText().split("\\s+");
            String[] n2list = in2.getText().split("\\s+");
            in3.setText("" + (Double.parseDouble(n1list[0]) + Double.parseDouble(n2list[0])));
        });
        minus.setOnMousePressed(e -> {
            String[] n1list = in1.getText().split("\\s+");
            String[] n2list = in2.getText().split("\\s+");
            in3.setText("" + (Double.parseDouble(n1list[0]) - Double.parseDouble(n2list[0])));
        });
        multiply.setOnMousePressed(e -> {
            String[] n1list = in1.getText().split("\\s+");
            String[] n2list = in2.getText().split("\\s+");
            in3.setText("" + (Double.parseDouble(n1list[0]) * Double.parseDouble(n2list[0])));
        });
        divide.setOnMousePressed(e -> {
            String[] n1list = in1.getText().split("\\s+");
            String[] n2list = in2.getText().split("\\s+");
            in3.setText(Double.parseDouble(n2list[0]) != 0
                    ? ("" + (Double.parseDouble(n1list[0]) / Double.parseDouble(n2list[0])))
                    : "除数不能为0！");
        });
    }
}